package com.fasteat.menu.async;

import com.fasteat.menu.entity.Menu;
import com.fasteat.menu.entity.ReportMessage;
import com.fasteat.menu.entity.ReportType;
import com.fasteat.menu.entity.client.Message;
import com.fasteat.menu.mapper.MenuMapper;
import com.fasteat.menu.mapper.ReportTypeMapper;
import com.fasteat.menu.service.client.MessageClient;
import com.fasteat.menu.util.MyStrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * @author run
 * @since 2021/3/9 20:20
 */
@Component
public class ReportAsync {

    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

    @Autowired
    private MessageClient messageClient;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private ReportTypeMapper typeMapper;

    @Async("asyncTaskExecutor")
    public void pushMessageToReporter(ReportMessage reportMessage, Menu menu, int action){

        String desciption = new StringBuilder().append("您于")
                                            .append(format.format(reportMessage.getGmtCreate()))
                                            .append("所提交的对于菜谱【")
                                            .append(MyStrUtil.limitLength(menu.getMenuTitle(), 10))
                                            .append("】")
                                            .append("的举报请求已被受理。")
                                            .toString();

        String payload;
        if (action == 1){
            payload = "经审查，您所举报的菜谱已被封禁。";
        }else {
            payload = "经审查，您所举报的菜谱并不存在相关的违规信息。";
        }

        Message message = new Message(reportMessage.getReporterId()
                                        , payload
                                        , "您的举报信息已受理"
                                        , desciption);

        messageClient.pushMessageToUser(message);
    }

    @Async("asyncTaskExecutor")
    public void pushMessageToAuthor(ReportMessage reportMessage, Menu menu){
        ReportType reportType = typeMapper.selectById(reportMessage.getReportId());

        String desciption = new StringBuilder().append("您所发布的菜谱【")
                                                .append(MyStrUtil.limitLength(menu.getMenuTitle(),10))
                                                .append("】存在违规信息并被举报，已于")
                                                .append(format.format(menu.getGmtModified()))
                                                .append("被下架")
                                                .toString();

        String payload = new StringBuilder().append("违规原因：")
                                            .append(reportType == null ? reportType.getReportName() : "其他")
                                            .append("举报描述：")
                                            .append(reportMessage.getReportDescription())
                                            .toString();

        Message message = new Message(reportMessage.getReporterId()
                                        , payload
                                        , "您的菜谱已被下架"
                                        , desciption);

        messageClient.pushMessageToUser(message);

    }

    @Async("asyncTaskExecutor")
    public void pushMessage(ReportMessage reportMessage, Integer action){
        if (action == null){
            action = 0;
        }
        Menu menu = menuMapper.selectById(reportMessage.getMenuId());
        if (menu == null){
            return;
        }

        reportMessage.setMenuTitle(menu.getMenuTitle());

        pushMessageToReporter(reportMessage, menu,action);
        pushMessageToAuthor(reportMessage, menu);

    }
}
